719B - Anatoly and Cockroaches - CodeForces Solution


greedy *1400

Please click on ads to support us..

Python Code:

input()
S=4*[0]
e=0
for c in input():
	S[e|('r'!=c)<<1]+=1
	e=not e
print(min(max(S[0],S[3]),max(S[1],S[2])))

C++ Code:

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define lln long long int
#define llu unsigned lln
#define sc(n) scanf("%d",&n);
#define scl(n) scanf("%lld",&n);
#define scd(n) scanf("%lf",&n);
#define pf(res) printf("%d\n",res);
#define pfl(res) printf("%lld\n",res);
#define pfd(res) printf("%lf\n",res);
#define pb push_back
#define endl "\n";
#define maxii 300005
using namespace std;
inline int setBit(int N, int pos) { return N = N | (1 << pos); }
inline int toggleBit(int N, int pos) { return N = (N ^ (1 << pos)); }
inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); }
typedef pair<int,int> pii;
typedef pair<lln,lln> pll;
vector<int> vi[maxii];
vector<int>:: iterator child;
vector<int>vi2[maxii];
vector<int>vi3[maxii];
vector<lln> vl;
vector<vector<pair<int, int> > > vii;
//vector<pii> vii;
vector<pll> vll;
using namespace __gnu_pbds;
//using namespace std;
typedef tree<pll, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update>ordered_set;
//ordered_set st;
//ordered_set :: iterator it;
//vii.clear();
//vii.resize(n + 1);
/* 1)find a small value than x for pbds st.order_of_key(x)
   2)find a number by position st.find_by_order(2)
   3)delete st.erase(st.find_by_order(st.order_of_key(tas2)));*/


int arr[maxii];
int arr2[4*maxii];
bool check[maxii];
bool check2[1000][1000];
int n,m;

string s;

int dp[maxii][2];



int main()
{
    //ios_base::sync_with_stdio(false);
   // cin.tie(NULL),cout.tie(NULL);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int test=1;
   // sc(test)
    for(int xx=1;xx<=test;xx++)
    {
        sc(n)
        //string a;
        cin>>s;
        int a=0,b=0,c=0,d=0;
        for(int i=0;i<n;i++)
        {
            if(i%2==1 && s[i]!='b')a++;
            if(i%2==0 && s[i]!='r')c++;
        }
        int ans1=max(a,c);

        for(int i=0;i<n;i++)
        {
            if(i%2==0 && s[i]!='b')b++;
            if(i%2==1 && s[i]!='r')d++;
        }
        int ans2=max(b,d);
        cout<<min(ans1,ans2);




    }


}


Comments

Submit
0 Comments
More Questions

1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K